home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / beos / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / Import-Export / ULT.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-26  |  11.6 KB  |  446 lines

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "ULT.h"
  25. #include "MAD.h"
  26. #include "RDriver.h"
  27.  
  28. #if defined(powerc) || defined(__powerc)
  29. enum {
  30.         PlayerPROPlug = kCStackBased
  31.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  32.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  33.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr)))
  34.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADMusic*)))
  35.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  36.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( MADDriverSettings*)))
  37. };
  38.  
  39. ProcInfoType __procinfo = PlayerPROPlug;
  40. #else
  41. #include <A4Stuff.h>
  42. #endif
  43.  
  44. #define LOW(para) ((para) & 15)
  45. #define HI(para) ((para) >> 4)
  46.  
  47. unsigned long Tdecode32( void *msg_buf)
  48. {
  49.   unsigned char *buf = msg_buf;
  50.   
  51.   return( (unsigned long) buf[3] << 24) | ( (unsigned long) buf[2] << 16) | ( (unsigned long) buf[ 1] << 8) | ( (unsigned long) buf[0]);
  52. }
  53.  
  54. short Tdecode16( void *msg_buf)
  55. {
  56.   unsigned char *buf = msg_buf;
  57.   
  58.   return ( (short) buf[1] << 8) | ( (short) buf[0]);
  59. }
  60.  
  61. Cmd* GetMADCommand( register short PosX, register short    TrackIdX, register PatData*    tempMusicPat)
  62. {
  63.     if( PosX < 0) PosX = 0;
  64.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  65.         
  66.     return( & (tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  67. }
  68.  
  69. Ptr MADPlugNewPtr( long size, MADDriverSettings* init)
  70. {
  71.     if( init->sysMemory) return NewPtrSys( size);
  72.     else return NewPtr( size);
  73. }
  74.  
  75. Ptr MADPlugNewPtrClear( long size, MADDriverSettings* init)
  76. {
  77.     if( init->sysMemory) return NewPtrSysClear( size);
  78.     else return NewPtrClear( size);
  79. }
  80.  
  81. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  82. {
  83.     register short len, i;
  84.     
  85.     len = *s2;
  86.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  87. }
  88.  
  89. void strncpy( Ptr dst, Ptr str, long size)
  90. {
  91.     BlockMove( str, dst, size);
  92. }
  93.  
  94.  
  95. void mystrcpy( Ptr a, Ptr b)
  96. {
  97.     BlockMove( b + 1, a, b[ 0]);
  98. }
  99.  
  100. OSErr ConvertULT2Mad( Ptr theULT, long MODSize, MADMusic *theMAD, MADDriverSettings *init)
  101. {
  102.     long                 i, PatMax, x, z, channel, Row;
  103.     long                 sndSize, starting, RES;
  104.     Ptr                    MaxPtr;
  105.     OSErr                theErr;
  106.     Ptr                    theInstrument[ 64], destPtr;
  107.     Byte                tempChar, *theULTCopy;
  108.     short                Note, Octave, maxTrack;
  109.     
  110.     /**** Variables pour le MAD ****/
  111.     Cmd                *aCmd;
  112.  
  113.     /**** Variables pour le ULT ****/
  114.     
  115.     ULTForm            ULTinfo;
  116.     ULTSuite        ULTSuite;
  117.     /********************************/
  118.  
  119.     for( i = 0 ; i < 64; i ++)
  120.     {
  121.         theInstrument[ i] = 0L;
  122.     }
  123.  
  124.     /**** Header principal *****/
  125.     theULTCopy = (Byte*) theULT;
  126.     
  127.     BlockMove( theULTCopy, &ULTinfo, sizeof( ULTinfo));
  128.     
  129. //    if( ULTinfo.reserved != 0) return MADFileNotSupportedByThisPlug;    // RES in v.1.4 see doc
  130.     
  131.     ULTSuite.NOS = *(theULTCopy + sizeof( ULTinfo) + ULTinfo.reserved * 32L);
  132.     
  133.     
  134.     /**** Ins Num *****/
  135.     if( sizeof( ULTIns) != 64) DebugStr("\pULTIns != 64");
  136.     ULTSuite.ins = (ULTIns*) NewPtrClear( ULTSuite.NOS * sizeof( ULTIns));
  137.     BlockMove( theULTCopy + sizeof( ULTinfo) + ULTinfo.reserved * 32L + 1, ULTSuite.ins, ULTSuite.NOS * sizeof( ULTIns));
  138.     
  139.     /**** Copy last infos *****/
  140.     BlockMove( theULTCopy + sizeof( ULTinfo) + ULTinfo.reserved * 32L + 1 + (ULTSuite.NOS * sizeof( ULTIns)), &ULTSuite.pattSeq, 256 + 2);
  141.     
  142.     // ******** Le ULT a ÄtÄ lu et analysÄ ***********
  143.     // ******** Copie des informations dans le MAD ***
  144.     
  145.     theMAD->header = (MADSpec*) MADPlugNewPtrClear( sizeof( MADSpec), init);
  146.     if( theMAD->header == 0L) return MADNeedMemory;
  147.         
  148.     theMAD->header->MAD = 'MADI';
  149.     for(i=0; i<32; i++) theMAD->header->name[i] = 0;
  150.     for(i=0; i<32; i++) theMAD->header->name[i] = ULTinfo.name[i];
  151.     
  152.     mystrcpy( theMAD->header->infos, (Ptr) "\pConverted by PlayerPRO ULT Plug (⌐Antoine ROSSET <rossetantoine@bluewin.ch>)");
  153.     
  154.     theMAD->header->numPat            = ULTSuite.NOP;
  155.     theMAD->header->numPointers    = 1;                    // CHANGE
  156.     theMAD->header->speed                = 6;
  157.     theMAD->header->tempo                = 125;
  158.     
  159.     for(i=0; i<128; i++) theMAD->header->oPointers[ i] = 0;
  160.     for(i=0; i<128; i++)
  161.     {
  162.         theMAD->header->oPointers[ i] = ULTSuite.pattSeq[i];
  163.         
  164.         if( theMAD->header->oPointers[ i] < 0 || theMAD->header->oPointers[ i] >= 128) theMAD->header->oPointers[ i] = 0;
  165.     }
  166.  
  167. for( i = 0; i < MAXTRACK; i++)
  168. {
  169.     if( i % 2 == 0) theMAD->header->chanPan[ i] = MAX_PANNING/4;
  170.     else theMAD->header->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  171.     
  172.     theMAD->header->chanVol[ i] = MAX_VOLUME;
  173. }
  174.  
  175.     theMAD->header->generalVol        = 64;
  176.     theMAD->header->generalSpeed    = 80;
  177.     theMAD->header->generalPitch    = 80;
  178.  
  179.     // ********************
  180.     // ***** INSTRUMENTS *****
  181.     // ********************
  182.     
  183.     theMAD->fid = ( InstrData*) MADPlugNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, init);
  184.     if( !theMAD->fid) return MADNeedMemory;
  185.     
  186.     theMAD->sample = ( sData**) MADPlugNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, init);
  187.     if( !theMAD->sample) return MADNeedMemory;
  188.     
  189.     for( i = 0; i < MAXINSTRU; i++) theMAD->fid[ i].firstSample = i * MAXSAMPLE;
  190.     
  191.     for(i  = 0 ; i < MAXINSTRU; i++)
  192.     {
  193.         for( x = 0; x < MAXSAMPLE; x++) theMAD->sample[ i*MAXSAMPLE + x] = 0L;
  194.         
  195.         theMAD->fid[i].numSamples    = 0;
  196.     }
  197.     
  198.     for(i=0; i<ULTSuite.NOS; i++)
  199.     {
  200.         InstrData        *curIns = &theMAD->fid[ i];
  201.         
  202.         curIns->type    = 0;
  203.         
  204.         {
  205.             sData    *curData;
  206.             
  207.             curIns->numSamples    = 1;
  208.             curIns->volFade            = DEFAULT_VOLFADE;
  209.             
  210.             curData = theMAD->sample[ i*MAXSAMPLE +  0] = (sData*) MADPlugNewPtrClear( sizeof( sData), init);
  211.             if( curData == 0L) return MADNeedMemory;
  212.             
  213.             ULTSuite.ins[i].loopStart    = Tdecode32( &ULTSuite.ins[i].loopStart);
  214.             ULTSuite.ins[i].loopEnd        = Tdecode32( &ULTSuite.ins[i].loopEnd);
  215.             ULTSuite.ins[i].sizeStart    = Tdecode32( &ULTSuite.ins[i].sizeStart);
  216.             ULTSuite.ins[i].sizeEnd        = Tdecode32( &ULTSuite.ins[i].sizeEnd);
  217.             ULTSuite.ins[i].finetune    = Tdecode16( &ULTSuite.ins[i].finetune);
  218.             
  219.             curData->size            = ULTSuite.ins[i].sizeEnd - ULTSuite.ins[i].sizeStart;        // * 2 ???
  220.             curData->loopBeg     = ULTSuite.ins[i].loopStart;
  221.             curData->loopSize    = ULTSuite.ins[i].loopEnd - ULTSuite.ins[i].loopStart;
  222.             curData->vol            = ULTSuite.ins[i].volume;
  223.             curData->c2spd        = ULTSuite.ins[i].finetune;
  224.             curData->loopType    = 0;
  225.             
  226.             switch( ULTSuite.ins[i].Bidi)
  227.             {
  228.                 case 4:
  229.                 case 12:
  230.                 case 28:
  231.                     curData->amp = 16;
  232.                 break;
  233.                 
  234.                 default:
  235.                     curData->amp            = 8;
  236.                 break;
  237.                 
  238.             }
  239.             
  240.             
  241.             curData->relNote    = 0;
  242.             for( x = 0; x < 28; x++) theMAD->fid[i].name[x] = ULTSuite.ins[i].name[x];
  243.             
  244.             curData->data         = MADPlugNewPtr( curData->size, init);
  245.             if( curData->data == 0L) return MADNeedMemory;
  246.             
  247.             if( curData->data == 0L) DebugStr("\pInstruments: I NEED MEMORY !!! NOW !");
  248.             
  249.             if( curData->data != 0L)
  250.             {
  251.                 BlockMove( theULT + ULTSuite.ins[i].sizeStart, curData->data, curData->size);
  252.             }
  253.         }
  254.     //    else curIns->numSamples = 0;
  255.     }
  256.     
  257.     theMAD->header->numChn = ULTSuite.NOC;
  258.     
  259.     for( i = 0; i < MAXPATTERN; i++) theMAD->partition[ i] = 0L;
  260.     for( i = 0; i < theMAD->header->numPat ; i++)
  261.     {
  262.         theMAD->partition[ i] = (PatData*) MADPlugNewPtrClear( sizeof( PatHeader) + theMAD->header->numChn * 64 * sizeof( Cmd), init);
  263.         if( theMAD->partition[ i] == 0L) return MADNeedMemory;
  264.         
  265.         theMAD->partition[ i]->header.size             = 64;
  266.         theMAD->partition[ i]->header.compMode     = 'NONE';
  267.         
  268.         for( x = 0; x < 20; x++) theMAD->partition[ i]->header.name[ x] = 0;
  269.         
  270.         MaxPtr = (Ptr) theMAD->partition[ i];
  271.         MaxPtr += sizeof( PatHeader) + theMAD->header->numChn * 64 * sizeof( Cmd);
  272.         
  273.         for( Row = 0; Row < 64; Row++)
  274.         {
  275.             for(z = 0; z < theMAD->header->numChn; z++)
  276.             {
  277.                 aCmd = GetMADCommand( Row, z, theMAD->partition[ i]);
  278.                 
  279.                 aCmd->note        = 0xFF;
  280.                 aCmd->ins            = 0;
  281.                 aCmd->cmd        = 0;
  282.                 aCmd->arg        = 0;
  283.                 aCmd->vol        = 0xFF;
  284.             }
  285.         }
  286.     }
  287.     
  288.     DisposePtr( (Ptr) ULTSuite.ins);
  289.     
  290.     return noErr;
  291. }
  292.  
  293. OSErr ExtractULTInfo( PPInfoRec *info, Ptr AlienFile)
  294. {
  295.     short        i, maxInstru, tracksNo;
  296.     ULTForm        ULTinfo;
  297.     /********************************/
  298.  
  299.     /**** Header principal *****/
  300.     BlockMove( AlienFile, &ULTinfo, 49);
  301.     
  302.     /*** Signature ***/
  303.     
  304.     info->signature = 'ULT ';
  305.     
  306.     /*** Internal name ***/
  307.     
  308.     ULTinfo.name[ 31] = '\0';
  309.     pStrcpy( (unsigned char*) info->internalFileName, CtoPstr( ULTinfo.name));
  310.     
  311.     /*** Total Patterns ***/
  312.     
  313.     info->totalPatterns = 0;    //Tdecode16(  &ITinfo.patNum);
  314.     
  315.     /*** Partition Length ***/
  316.     
  317.     info->partitionLength = 0;    //Tdecode16( &ITinfo.orderNum);
  318.     
  319.     /*** Total Instruments ***/
  320.     
  321.     info->totalInstruments = 0;
  322.     
  323.     /*** Tracks ***/
  324.     
  325.     info->totalTracks     = 0;
  326.     
  327.     pStrcpy( info->formatDescription, "ULT Plug");
  328.  
  329.     return noErr;
  330. }
  331.  
  332. OSErr TestULTFile( Ptr AlienFile)
  333. {
  334.     ULTForm    *myULT = ( ULTForm*) AlienFile;
  335.  
  336.     if( *((long *) myULT->ID) == 'MAS_') return   noErr;
  337.     else return  MADFileNotSupportedByThisPlug;
  338. }
  339.  
  340. OSErr TEST2main( OSType order, char *AlienFileFSSpec, MADMusic *MadFile, PPInfoRec *info, MADDriverSettings *init)
  341. {
  342.     OSErr    myErr;
  343.     Ptr        AlienFile;
  344.     short    vRefNum, iFileRefI;
  345.     long        dirID, sndSize;
  346.     
  347. #ifndef powerc
  348.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  349. #endif
  350.  
  351.     HGetVol( 0L, &vRefNum, &dirID);
  352.     HSetVol( 0L, AlienFileFSSpec->vRefNum, AlienFileFSSpec->parID);
  353.  
  354.     myErr = noErr;
  355.  
  356.     switch( order)
  357.     {
  358.         case 'IMPL':
  359.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  360.             if( myErr == noErr)
  361.             {
  362.                 GetEOF( iFileRefI, &sndSize);
  363.             
  364.                 // ** MEMORY Test Start
  365.                 AlienFile = MADPlugNewPtr( sndSize * 2L, init);
  366.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  367.                 // ** MEMORY Test End
  368.                 
  369.                 else
  370.                 {
  371.                     DisposePtr( AlienFile);
  372.                     
  373.                     AlienFile = MADPlugNewPtr( sndSize, init);
  374.                     if( AlienFile == 0L) myErr = MADNeedMemory;
  375.                     else
  376.                     {
  377.                         myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  378.                         if( myErr == noErr)
  379.                         {
  380.                             myErr = TestULTFile( AlienFile);
  381.                             if( myErr == noErr)
  382.                             {
  383.                                 myErr = ConvertULT2Mad( AlienFile,  GetPtrSize( AlienFile), MadFile, init);
  384.                             }
  385.                         }
  386.                     }
  387.                     DisposePtr( AlienFile);    AlienFile = 0L;
  388.                 }
  389.                 FSClose( iFileRefI);
  390.             }
  391.         break;
  392.         
  393.         case 'TEST':
  394.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  395.             if( myErr == noErr)
  396.             {
  397.                 sndSize = 1024L;
  398.                 
  399.                 AlienFile = MADPlugNewPtr( sndSize, init);
  400.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  401.                 else
  402.                 {
  403.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  404.                     myErr = TestULTFile( AlienFile);
  405.                     
  406.                     DisposePtr( AlienFile);    AlienFile = 0L;
  407.                 }
  408.                 FSClose( iFileRefI);
  409.             }
  410.         break;
  411.  
  412.         case 'INFO':
  413.             myErr = FSOpen( AlienFileFSSpec->name, 0, &iFileRefI);
  414.             if( myErr == noErr)
  415.             {
  416.                 GetEOF( iFileRefI, &info->fileSize);
  417.             
  418.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  419.                 
  420.                 AlienFile = MADPlugNewPtr( sndSize, init);
  421.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  422.                 else
  423.                 {
  424.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  425.                     if( myErr == noErr)
  426.                     {
  427.                         myErr = ExtractULTInfo( info, AlienFile);
  428.                     }
  429.                     DisposePtr( AlienFile);    AlienFile = 0L;
  430.                 }
  431.                 FSClose( iFileRefI);
  432.             }
  433.         break;
  434.         
  435.         default:
  436.             myErr = MADOrderNotImplemented;
  437.         break;
  438.     }
  439.  
  440.     HSetVol( 0L, vRefNum, dirID);
  441.  
  442.     #ifndef powerc
  443.         SetA4( oldA4);
  444.     #endif
  445.     return myErr;
  446. }